/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

/* eslint-disable eqeqeq */
import type { Effect, Reducer } from 'umi';
import {
  getPddComplainDetail,
  passRecordToPDD,
  getComplaintTypeInfo,
  pddComplaintSubmit,
  getPddComplainList,
} from '@/services/customerServiceTools';
import { message } from 'antd';
import type { PddComplaintDeatil, PddComplaintTypes } from '../types/PDDComplaintOrder';

interface PddComplaintTypesItem {
  label: string;
  value: string;
}
export interface PddComplaintState {
  pddComplaintDetail: PddComplaintDeatil;
  pddComplaintTypes: PddComplaintTypesItem[]; // 投诉类型
  todo_page_ids: string[];
  secondComplaint_page_ids: string[];
  finished_page_ids: string[];
}

export interface PddComplaint {
  namespace: string;
  state: PddComplaintState;
  effects: {
    // releaseNotice: Effect;
    getPddComplainList: Effect;
    getPddDetail: Effect;
    passRecordToPDD: Effect;
    getComplaintTypeInfo: Effect;
    pddComplaintSubmit: Effect;
  };
  reducers: {
    save: Reducer<PddComplaintState>;
  };
}

const Model: PddComplaint = {
  namespace: 'pddComplaint',

  state: {
    pddComplaintDetail: {
      complaint: {},
      pdd_gd: {},
      inn: {},
      record: [],
    },
    pddComplaintTypes: [],
    todo_page_ids: [],
    secondComplaint_page_ids: [],
    finished_page_ids: [],
  },

  effects: {
    *getPddComplainList({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getPddComplainList, payload);
      if (!response) return;
      const { code, msg, data } = response;
      if (code == 0) {
        yield put({
          type: 'save',
          payload: {
            pddComplaintList: data,
          },
        });
        __dva_resolve(response);
      } else {
        message.error(msg);
        __dva_reject(response);
      }
    },
    *getPddDetail({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getPddComplainDetail, payload);
      if (!response) return;
      const { code, msg, data } = response;
      if (code == 0) {
        yield put({
          type: 'save',
          payload: {
            pddComplaintDetail: data,
          },
        });
        __dva_resolve(response);
      } else {
        message.error(msg);
        __dva_reject(response);
      }
    },
    // 直接提交给拼多多
    *passRecordToPDD({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(passRecordToPDD, payload);
      if (!response) return;
      const { code, msg } = response;
      if (code == 0) {
        // message.success(msg);
        __dva_resolve(response);
      } else {
        message.error(msg);
        __dva_reject(response);
      }
    },
    // 拼多多投诉提交
    *pddComplaintSubmit({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(pddComplaintSubmit, payload);
      if (!response) return;
      const { code, msg } = response;
      if (code == 0) {
        __dva_resolve(response);
      } else {
        message.error(msg);
        __dva_reject(response);
      }
    },
    // 获取拼多多投诉类型下拉列表
    *getComplaintTypeInfo({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getComplaintTypeInfo, payload);
      if (!response) return;
      const { code, msg, data }: { code: any; msg: string; data: PddComplaintTypes } = response;

      if (code == 0) {
        yield put({
          type: 'save',
          payload: {
            pddComplaintTypes: data,
          },
        });
        __dva_resolve(response);
      } else {
        message.error(msg);
        __dva_reject(response);
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};

export default Model;
